//
//  CouponViewController.m
//  KiklinkApp
//
//  Created by LJC on 14/10/21.
//  Copyright (c) 2014年 Kiklink. All rights reserved.
//

#import "CouponViewController.h"

#import "ImagesManager.h"
#import "UIColor+hex.h"
#import "UIView+loadWithNib.h"

#import "MJRefresh.h"
#import "CouponCell.h"

#import "API.h"

NSString *const couponTableCellIdentifier1 = @"CouponCell";
NSString *const couponTableCellIdentifier2 = @"CouponCell";
NSString *const couponTableCellIdentifier3 = @"CouponCell";

@interface CouponViewController ()
{
    //data
    NSMutableArray *couponTableArray1;
    NSMutableArray *couponTableArray2;
    NSMutableArray *couponTableArray3;
    NSInteger page1;
    NSInteger page2;
    NSInteger page3;
    NSInteger totalPage1;
    NSInteger totalPage2;
    NSInteger totalPage3;
}
@end

@implementation CouponViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"优惠券";
    self.view.backgroundColor = [UIColor colorWithHexString:AppColor_BackGround];

    
    //loading
    [self.activity1 startAnimating];
    [self.activity2 startAnimating];
    [self.activity3 startAnimating];
    self.refreshBtn1.hidden = YES;
    self.refreshBtn2.hidden = YES;
    self.refreshBtn3.hidden = YES;
    [self.refreshBtn1 addTarget:self action:@selector(refresh1) forControlEvents:UIControlEventTouchUpInside];
    [self.refreshBtn2 addTarget:self action:@selector(refresh2) forControlEvents:UIControlEventTouchUpInside];
    [self.refreshBtn3 addTarget:self action:@selector(refresh3) forControlEvents:UIControlEventTouchUpInside];
    
    
    //BundleImages
    ImagesManager *imagesManager = [ImagesManager sharedImagesManager];
    
    
    //leftNavItem
    UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [backButton setFrame:CGRectMake(0, 0, NavBarHeight/4, NavBarHeight/2)];
    [backButton setBackgroundImage:[imagesManager back] forState:UIControlStateNormal];
    [backButton setBackgroundImage:[imagesManager backTapped] forState:UIControlStateHighlighted];
    [backButton addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *leftNavBarItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
    self.navigationItem.leftBarButtonItem = leftNavBarItem;
    
    
    //segment
    self.segment.tintColor = [UIColor colorWithHexString:AppColor_Main];
    [self.segment addTarget:self action:@selector(segmentAction) forControlEvents:UIControlEventValueChanged];
    
    
    //couponTable
    self.couponTable1.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.couponTable1.backgroundColor = [UIColor colorWithHexString:AppColor_BackGround];
    self.couponTable1.delegate = self;
    self.couponTable1.dataSource = self;
    [self.couponTable1 registerClass:[UITableViewCell class] forCellReuseIdentifier:couponTableCellIdentifier1];
    
    self.couponTable2.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.couponTable2.backgroundColor = [UIColor colorWithHexString:AppColor_BackGround];
    self.couponTable2.delegate = self;
    self.couponTable2.dataSource = self;
    [self.couponTable2 registerClass:[UITableViewCell class] forCellReuseIdentifier:couponTableCellIdentifier2];
    
    self.couponTable3.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.couponTable3.backgroundColor = [UIColor colorWithHexString:AppColor_BackGround];
    self.couponTable3.delegate = self;
    self.couponTable3.dataSource = self;
    [self.couponTable3 registerClass:[UITableViewCell class] forCellReuseIdentifier:couponTableCellIdentifier3];
    
    [self setupRefresh];
    
    
    //data
    couponTableArray1 = [[NSMutableArray alloc] init];
    couponTableArray2 = [[NSMutableArray alloc] init];
    couponTableArray3 = [[NSMutableArray alloc] init];
    page1 = 1;
    page2 = 1;
    page3 = 1;
    [self getData:1 Condition:@"first" Style:@"spendable"];
    [self getData:1 Condition:@"first" Style:@"used"];
    [self getData:1 Condition:@"first" Style:@"overdue"];
}

#pragma mark - get data
//condition:first&header&footer  style:spendable&used&overdue
- (void)getData:(NSInteger)pageNum Condition:(NSString *)condition Style:(NSString *)style
{
    NSLog(@"page:%ld",(long)pageNum);
    //    [API courseSearchWithPageNumber:[NSNumber numberWithInteger:pageNum] Keyword:@"" SortStyle:sortStyle Distance:@0 Tags:@"" Callback:^(int stateCode, NSString *word, id content) {
    //
    //        if (stateCode == SUCCESS) {
    //            ProductPage *productPage = (ProductPage *)content;
    //            NSMutableArray *result = [[NSMutableArray alloc] initWithArray:productPage.result];
    //
    //            if ([sortStyle isEqualToString:@""]) {
    //
    //                if ([condition isEqualToString:@"first"]) {
    //                    totalPage1 = productPage.lastPageNumber;
    //                    self.localTable1.scrollEnabled = YES;
    //                    page1 = 2;
    //                    localTableArray1 = result;
    //                } else if ([condition isEqualToString:@"header"]) {
    //                    page1 = 2;
    //                    localTableArray1 = result;
    //                } else if ([condition isEqualToString:@"footer"]) {
    //                    page1 ++;
    //                    [localTableArray1 addObjectsFromArray:result];
    //                }
    //
    //                [self loadData1:condition];
    //
    //            } else if ([sortStyle isEqualToString:@"price"]) {
    //
    //                if ([condition isEqualToString:@"first"]) {
    //                    totalPage2 = productPage.lastPageNumber;
    //                    self.localTable2.scrollEnabled = YES;
    //                    page2 = 2;
    //                    localTableArray2 = result;
    //                } else if ([condition isEqualToString:@"header"]) {
    //                    page2 = 2;
    //                    localTableArray2 = result;
    //                } else if ([condition isEqualToString:@"footer"]) {
    //                    page2 ++;
    //                    [localTableArray2 addObjectsFromArray:result];
    //                }
    //
    //                [self loadData2:condition];
    //
    //            }
    //
    //        } else {
    //
    //            if ([sortStyle isEqualToString:@""]) {
    //
    //                if ([condition isEqualToString:@"first"]) {
    //                    self.localTable1.scrollEnabled = NO;
    //                    [self.activity1 stopAnimating];
    //                    self.refreshBtn1.hidden = NO;
    //                } else if ([condition isEqualToString:@"header"]) {
    //                    [self.localTable1 headerEndRefreshing];
    //                } else if ([condition isEqualToString:@"footer"]) {
    //                    [self.localTable1 footerEndRefreshing];
    //                }
    //
    //            } else if ([sortStyle isEqualToString:@"price"]) {
    //
    //                if ([condition isEqualToString:@"first"]) {
    //                    self.localTable2.scrollEnabled = NO;
    //                    [self.activity2 stopAnimating];
    //                    self.refreshBtn2.hidden = NO;
    //                } else if ([condition isEqualToString:@"header"]) {
    //                    [self.localTable2 headerEndRefreshing];
    //                } else if ([condition isEqualToString:@"footer"]) {
    //                    [self.localTable2 footerEndRefreshing];
    //                }
    //                
    //            }
    //            
    //        }
    //        
    //    }];
}

- (void)loadData1:(NSString *)condition
{
    [self.couponTable1 reloadData];
    
    if ([condition isEqualToString:@"first"]) {
        [self.activity1 stopAnimating];
    } else if ([condition isEqualToString:@"header"]) {
        [self.couponTable1 headerEndRefreshing];
    } else if ([condition isEqualToString:@"footer"]) {
        [self.couponTable1 footerEndRefreshing];
    }
}
- (void)loadData2:(NSString *)condition
{
    [self.couponTable2 reloadData];
    
    if ([condition isEqualToString:@"first"]) {
        [self.activity2 stopAnimating];
    } else if ([condition isEqualToString:@"header"]) {
        [self.couponTable2 headerEndRefreshing];
    } else if ([condition isEqualToString:@"footer"]) {
        [self.couponTable2 footerEndRefreshing];
    }
}
- (void)loadData3:(NSString *)condition
{
    [self.couponTable3 reloadData];
    
    if ([condition isEqualToString:@"first"]) {
        [self.activity3 stopAnimating];
    } else if ([condition isEqualToString:@"header"]) {
        [self.couponTable3 headerEndRefreshing];
    } else if ([condition isEqualToString:@"footer"]) {
        [self.couponTable3 footerEndRefreshing];
    }
}

- (void)refresh1
{
    self.refreshBtn1.hidden = YES;
    [self.activity1 startAnimating];
    
    [self getData:1 Condition:@"first" Style:@"spendable"];
}
- (void)refresh2
{
    self.refreshBtn2.hidden = YES;
    [self.activity2 startAnimating];
    
    [self getData:1 Condition:@"first" Style:@"used"];
}
- (void)refresh3
{
    self.refreshBtn3.hidden = YES;
    [self.activity3 startAnimating];
    
    [self getData:1 Condition:@"first" Style:@"overdue"];
}
#pragma mark - get data End

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.tabBarController.tabBar.hidden = YES;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

#pragma mark - back
- (void)back
{
    [self.navigationController popViewControllerAnimated:YES];
}
#pragma mark - back End

#pragma mark - segmentAction
- (void)segmentAction
{
    NSInteger selectedIndex = self.segment.selectedSegmentIndex;
    switch (selectedIndex) {
        case 0:
            [self.couponTable1 setHidden:NO];
            [self.couponTable2 setHidden:YES];
            [self.couponTable3 setHidden:YES];
            
            [self.loadingView1 setHidden:NO];
            [self.loadingView2 setHidden:YES];
            [self.loadingView3 setHidden:YES];
            break;
        case 1:
            [self.couponTable1 setHidden:YES];
            [self.couponTable2 setHidden:NO];
            [self.couponTable3 setHidden:YES];
            
            [self.loadingView1 setHidden:YES];
            [self.loadingView2 setHidden:NO];
            [self.loadingView3 setHidden:YES];
            break;
        case 2:
            [self.couponTable1 setHidden:YES];
            [self.couponTable2 setHidden:YES];
            [self.couponTable3 setHidden:NO];
            
            [self.loadingView1 setHidden:YES];
            [self.loadingView2 setHidden:YES];
            [self.loadingView3 setHidden:NO];
            break;
        default:
            break;
    }
}
#pragma mark - segmentAction End

#pragma tableView dataSource&delegate  &MJRefresh
- (void)setupRefresh
{
    [self.couponTable1 addHeaderWithTarget:self action:@selector(headerRereshing)];
    [self.couponTable1 addFooterWithTarget:self action:@selector(footerRereshing)];
    self.couponTable1.headerPullToRefreshText = @"下拉可以刷新";
    self.couponTable1.headerReleaseToRefreshText = @"松开马上刷新";
    self.couponTable1.headerRefreshingText = @"正在刷新...";
    self.couponTable1.footerPullToRefreshText = @"上拉加载更多";
    self.couponTable1.footerReleaseToRefreshText = @"松开马上加载";
    self.couponTable1.footerRefreshingText = @"正在加载...";
    
    
    [self.couponTable2 addHeaderWithTarget:self action:@selector(headerRereshing)];
    [self.couponTable2 addFooterWithTarget:self action:@selector(footerRereshing)];
    self.couponTable2.headerPullToRefreshText = @"下拉可以刷新";
    self.couponTable2.headerReleaseToRefreshText = @"松开马上刷新";
    self.couponTable2.headerRefreshingText = @"正在刷新...";
    self.couponTable2.footerPullToRefreshText = @"上拉加载更多";
    self.couponTable2.footerReleaseToRefreshText = @"松开马上加载";
    self.couponTable2.footerRefreshingText = @"正在加载...";
    
    [self.couponTable3 addHeaderWithTarget:self action:@selector(headerRereshing)];
    [self.couponTable3 addFooterWithTarget:self action:@selector(footerRereshing)];
    self.couponTable3.headerPullToRefreshText = @"下拉可以刷新";
    self.couponTable3.headerReleaseToRefreshText = @"松开马上刷新";
    self.couponTable3.headerRefreshingText = @"正在刷新...";
    self.couponTable3.footerPullToRefreshText = @"上拉加载更多";
    self.couponTable3.footerReleaseToRefreshText = @"松开马上加载";
    self.couponTable3.footerRefreshingText = @"正在加载...";
}
- (void)headerRereshing
{
    //data
    if (self.segment.selectedSegmentIndex == 0) {
        [self getData:1 Condition:@"header" Style:@"spendable"];
    } else if (self.segment.selectedSegmentIndex == 1) {
        [self getData:1 Condition:@"header" Style:@"used"];
    } else if (self.segment.selectedSegmentIndex == 2) {
        [self getData:1 Condition:@"header" Style:@"overdue"];
    }
}
- (void)footerRereshing
{
    //data
    if (self.segment.selectedSegmentIndex == 0) {
        if (page1 <= totalPage1) {
            [self getData:page1 Condition:@"footer" Style:@"spendable"];
        } else {
            [self.couponTable1 footerEndRefreshing];
        }
    } else if (self.segment.selectedSegmentIndex == 1) {
        if (page2 <= totalPage2) {
            [self getData:page2 Condition:@"footer" Style:@"used"];
        } else {
            [self.couponTable2 footerEndRefreshing];
        }
    } else if (self.segment.selectedSegmentIndex == 2) {
        if (page3 <= totalPage3) {
            [self getData:page3 Condition:@"footer" Style:@"overdue"];
        } else {
            [self.couponTable3 footerEndRefreshing];
        }
    }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (tableView == self.couponTable1) {
        return couponTableArray1.count;
    } else if (tableView == self.couponTable2) {
        return couponTableArray2.count;
    } else if (tableView == self.couponTable3) {
        return couponTableArray3.count;
    }
    return 0;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView == self.couponTable1) {
        BOOL nibsRegistered = NO;
        if (!nibsRegistered) {
            UINib *nib = [UINib nibWithNibName:@"CouponCell" bundle:nil];
            [tableView registerNib:nib forCellReuseIdentifier:couponTableCellIdentifier1];
            nibsRegistered = YES;
        }
        CouponCell *cell = (CouponCell *)[tableView dequeueReusableCellWithIdentifier:couponTableCellIdentifier1];
//        [cell loadDataWithCoupon:[couponTableArray1 objectAtIndex:indexPath.row]];
        return cell;
    } else if (tableView == self.couponTable2) {
        BOOL nibsRegistered = NO;
        if (!nibsRegistered) {
            UINib *nib = [UINib nibWithNibName:@"CouponCell" bundle:nil];
            [tableView registerNib:nib forCellReuseIdentifier:couponTableCellIdentifier2];
            nibsRegistered = YES;
        }
        CouponCell *cell = (CouponCell *)[tableView dequeueReusableCellWithIdentifier:couponTableCellIdentifier2];
//        [cell loadDataWithCoupon:[couponTableArray2 objectAtIndex:indexPath.row]];
        return cell;
    } else if (tableView == self.couponTable3) {
        BOOL nibsRegistered = NO;
        if (!nibsRegistered) {
            UINib *nib = [UINib nibWithNibName:@"CouponCell" bundle:nil];
            [tableView registerNib:nib forCellReuseIdentifier:couponTableCellIdentifier3];
            nibsRegistered = YES;
        }
        CouponCell *cell = (CouponCell *)[tableView dequeueReusableCellWithIdentifier:couponTableCellIdentifier3];
//        [cell loadDataWithCoupon:[couponTableArray3 objectAtIndex:indexPath.row]];
        return cell;
    }
    return nil;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 55;
}
#pragma tableView dataSource&delegate End

@end
